package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.ProductQuestionMapper;
import com.yunhai.device.service.ProductQuestionService;
import com.yunhai.model.entity.dms.DmsProductQuestionEntity;
import com.yunhai.model.vo.dms.DmsProductQuestionQuery;
import com.yunhai.model.vo.dms.DmsProductQuestionVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductQuestionServiceImpl implements ProductQuestionService {

    @Resource
    private ProductQuestionMapper productQuestionMapper;

    /**
     * 获取 ProductQuestion 分页列表
     */
    @Transactional
    public IPage getProductQuestionPagination(PaginationVo paginationVo, DmsProductQuestionQuery dmsProductQuestionQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsProductQuestionEntity> lqs = new LambdaQueryWrapper();
        productQuestionMapper.selectPage(page, lqs);

        List<DmsProductQuestionEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 ProductQuestion 所有数据
     *
     */
    @Transactional
    public List<DmsProductQuestionEntity> getProductQuestionAllList( DmsProductQuestionQuery dmsProductQuestionQuery ) throws Exception {

        LambdaQueryWrapper<DmsProductQuestionEntity> lqs = new LambdaQueryWrapper();
        List<DmsProductQuestionEntity> list = productQuestionMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 ProductQuestion 详情
     */
    @Transactional
    public DmsProductQuestionEntity getProductQuestionDetail( Long id) throws Exception {

        DmsProductQuestionEntity dmsProductQuestionEntity = productQuestionMapper.selectById(id);
        return dmsProductQuestionEntity;
    }


    /**
     * 新增 ProductQuestion
     * */
    @Transactional
    public void addProductQuestion(DmsProductQuestionVo dmsProductQuestionVo) throws Exception {

        DmsProductQuestionEntity dmsProductQuestionEntity = new DmsProductQuestionEntity();
        ReflectionCopy.copyProperties( dmsProductQuestionEntity, dmsProductQuestionVo );
        productQuestionMapper.insert(dmsProductQuestionEntity);
        return;
    }


    /**
     * 更新 ProductQuestion
     * */
    @Transactional
    public void updateProductQuestion( DmsProductQuestionVo dmsProductQuestionVo) throws Exception {

        if( dmsProductQuestionVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsProductQuestionEntity dmsProductQuestionEntity = new DmsProductQuestionEntity();
        ReflectionCopy.copyProperties( dmsProductQuestionEntity, dmsProductQuestionVo );
        productQuestionMapper.updateById(dmsProductQuestionEntity);
        return;
    }

    /**
     * 删除 ProductQuestion
     * */
    @Transactional
    public void batchRemoveProductQuestion( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        productQuestionMapper.deleteBatchIds(ids);
    }

}